define(["dojo/_base/declare", "esri/layers/TiledMapServiceLayer", "esri/geometry/Polygon", "esri/geometry/Extent","esri/geometry/Point", "esri/SpatialReference", "esri/layers/TileInfo","esri/tileUtils"],
    function (declare, TiledMapServiceLayer, Polygon, Extent, Point, SpatialReference, TileInfo,tileUtils) {
        return declare(TiledMapServiceLayer, {
            // 构造函数
            constructor: function (properties) {
                this.spatialReference = new SpatialReference({
		           wkt: 'PROJCS["CGCS2000_3_Degree_GK_CM_117E",GEOGCS["GCS_China_Geodetic_Coordinate_System_2000",DATUM["D_China_2000",SPHEROID["CGCS2000",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",118.5],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]'
		        });
                // 图层提供的起始显示范围以及整个图层的地理范围
                this.fullExtent = new Extent({
							xmin: 510048.2226551423,
							ymin: 2749711.8657475044,
							xmax: 518814.2226551423,
							ymax: 2756239.8657475044,
							"spatialReference":this.spatialReference
						});
                this.initialExtent =new Extent({
							xmin: 506313.56111668097,
							ymin: 2750728.48113212,
							xmax: 522570.5457320652,
							ymax: 2756566.2657475043,
							"spatialReference":this.spatialReference
						});
				// 图层提供的切片信息
                this.tileInfo = new TileInfo({
                    "rows": 256,
                    "cols": 256,
                    "compressionQuality": 0,
                    "origin": {
                        x: -5123200,
                        y: 10002100
                    },
                    "spatialReference": {
                        wkt: 'PROJCS["CGCS2000_3_Degree_GK_CM_117E",GEOGCS["GCS_China_Geodetic_Coordinate_System_2000",DATUM["D_China_2000",SPHEROID["CGCS2000",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",118.5],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]'
                    },
                    "lods":
					[
					    {
					        "level": 0,
					        "resolution": 76.35145950305859,
					        "scale": 288571.868221,
					        "startTileRow": 370,
					        "startTileCol": 288,
					        "endTileRow": 371,
					        "endTileCol": 288
					    },
					    {
					        "level": 1,
					        "resolution": 30.34786090588223,
					        "scale": 114700.347251,
					        "startTileRow": 932,
					        "startTileCol": 725,
					        "endTileRow": 933,
					        "endTileCol": 726
					    },
					    {
					        "level": 2,
					        "resolution": 15.173930452808824,
					        "scale": 57350.173625,
					        "startTileRow": 1865,
					        "startTileCol": 1450,
					        "endTileRow": 1866,
					        "endTileCol": 1452
					    },
					    {
					        "level": 3,
					        "resolution": 7.586965226536704,
					        "scale": 28675.086813,
					        "startTileRow": 3730,
					        "startTileCol": 2900,
					        "endTileRow": 3733,
					        "endTileCol": 2904
					    },
					    {
					        "level": 4,
					        "resolution": 3.79348261313606,
					        "scale": 14337.543406,
					        "startTileRow": 7461,
					        "startTileCol": 5800,
					        "endTileRow": 7467,
					        "endTileCol": 5809
					    },
					    {
					        "level": 5,
					        "resolution": 1.89674130656803,
					        "scale": 7168.771703,
					        "startTileRow": 14922,
					        "startTileCol": 11601,
					        "endTileRow": 14935,
					        "endTileCol": 11619
					    },
					    {
					        "level": 6,
					        "resolution": 0.9483706534163069,
					        "scale": 3584.385852,
					        "startTileRow": 29845,
					        "startTileCol": 23202,
					        "endTileRow": 29871,
					        "endTileCol": 23238
					    },
					    {
					        "level": 7,
					        "resolution": 0.47418532670815344,
					        "scale": 1792.192926,
					        "startTileRow": 59690,
					        "startTileCol": 46405,
					        "endTileRow": 59743,
					        "endTileCol": 46477
					    },
					    {
					        "level": 8,
					        "resolution": 0.23709266335407672,
					        "scale": 896.096463,
					        "startTileRow": 119380,
					        "startTileCol": 92811,
					        "endTileRow": 119487,
					        "endTileCol": 92955
					    },
					    {
					        "level": 9,
					        "resolution": 0.11854633154474643,
					        "scale": 448.048231,
					        "startTileRow": 238760,
					        "startTileCol": 185622,
					        "endTileRow": 238975,
					        "endTileCol": 185911
					    },
					    {
					        "level": 10,
					        "resolution": 0.05291677250021167,
					        "scale": 200,
					        "startTileRow": 534880,
					        "startTileCol": 415839,
					        "endTileRow": 535362,
					        "endTileCol": 416486
					    },
					    {
					        "level": 11,
					        "resolution": 0.026458386250105836,
					        "scale": 100,
					        "startTileRow": 1069760,
					        "startTileCol": 831678,
					        "endTileRow": 1070724,
					        "endTileCol": 832972
					    },
					    {
					        "level": 12,
					        "resolution": 0.013229193125052918,
					        "scale": 50,
					        "startTileRow": 2139521,
					        "startTileCol": 1663357,
					        "endTileRow": 2141448,
					        "endTileCol": 1665945
					    }
					]
                });
                this.url = properties.url;
				this.region = properties.geometry;
                // 设置图层的loaded属性，并触发onLoad事件
                this.parseGeometry();
                this.loaded = true;
                this.onLoad(this);
            },
			parseGeometry(){
				this.levelsOfRegion = {}
				var lods = this.tileInfo.lods,n = lods.length,ring = this.region.rings[0];
            	for(var i=0;i<n;i++){
            		var m = ring.length,grids = [],gridIds = [];
            		lods[i].grids = []
            		this.levelsOfRegion[lods[i].level] = obj = {}
            		for(var j=0;j<m;j++){
            			var p = new Point(ring[j][0],ring[j][1],this.spatialReference);
            			var cell = tileUtils.getContainingTileCoords(this.tileInfo,p,lods[i]);
            			var gridId = cell.row + ',' +cell.col;            			
            			if(!gridIds.includes(gridId)){
            				gridIds.push(gridId);
            				grids.push([parseInt(cell.row),parseInt(cell.col)]);
            			}
            		}
//          		lods[i].grids = grids
					if(grids.length > 4){
						var polygonJson  = {"rings":[grids],"spatialReference":this.spatialReference};
						var polygon = new Polygon(polygonJson);
						obj.geometry = polygon;
						this.levelsOfRegion[lods[i].level] = obj
					}
            	}
			},
            getTileUrl: function (level, row, col) {
				var region = this.levelsOfRegion[level];
				var url = null;
				var point = new Point(row,col,this.spatialReference)
				if(region&&region.hasOwnProperty('geometry')&&region.geometry.contains(point)){
					//arcgis server sample like below
					//url = "http://183.250.128.116:5092/arcgis/rest/services/ysgw16/MapServer/tile/"+level+"/"+row+"/"+col;
                	url = this.url +"/tile/"+level+"/"+row+"/"+col;
                	return url;
				}            	
                //var url = "http://192.168.20.156:6080/arcgis/rest/services/donghai_img/MapServer/tile/"+level+"/"+row+"/"+col;
                //var url = "http://183.250.128.116:5092/arcgis/rest/services/ysgw16/MapServer/tile/"+level+"/"+row+"/"+col;
                return url
            }
        });
    }
)